﻿unit CmCustomerFileService;

interface
{$I ../Config.inc}
uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm,
  CmCustomerFileModel,
  CmCustomerFileBLL,
  CmEnterpriseFileService,
  CmEnterpriseFileModel,
  CmEnterpriseEmpModel,
  CmLinkmanModel;
  
type


  TCmCustomerFileService = class(TCmCustomerFileBLL)
  private

  protected

  public
  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TCmCustomerFile; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TCmCustomerFileList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TCmCustomerFile; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TCmCustomerFileList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function Check(const iItem:TCmCustomerFile; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TCmCustomerFileList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeExecute(const iItem:TCmCustomerFile; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TCmCustomerFileList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function AfterTreatment(const iItem:TCmCustomerFile; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TCmCustomerFileList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
  {$ENDIF}
    function LoadAll(const iOrgId:string; const iItems:TCmCustomerFileList; out oError:string; const iJustUse:Boolean=False):Boolean;
    function Use(const iItem:TCmCustomerFile; out oError:string):Boolean;
    function Add(const iItem:TCmCustomerFile; out oError:string; const dIsCheck:Boolean=True):Boolean;
    function Delete(const iItem:TCmCustomerFile; out oError:string; const dIsCheck:Boolean=True):Boolean;
  end;
  
implementation

{$IFDEF SERVER}
function TCmCustomerFileService.BeforeTreatment(const iItem:TCmCustomerFile; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmCustomerFileService.BeforeTreatment(const iItems:TCmCustomerFileList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmCustomerFileService.BeforeCheck(const iItem:TCmCustomerFile; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmCustomerFileService.BeforeCheck(const iItems:TCmCustomerFileList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmCustomerFileService.Check(const iItem:TCmCustomerFile; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmCustomerFileService.Check(const iItems:TCmCustomerFileList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmCustomerFileService.BeforeExecute(const iItem:TCmCustomerFile; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmCustomerFileService.BeforeExecute(const iItems:TCmCustomerFileList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmCustomerFileService.AfterTreatment(const iItem:TCmCustomerFile; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmCustomerFileService.AfterTreatment(const iItems:TCmCustomerFileList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;
{$ENDIF}

function TCmCustomerFileService.LoadAll(const iOrgId:string; const iItems:TCmCustomerFileList; out oError:string; const iJustUse:Boolean=False):Boolean;
var
  Condition,OrderBy:string;
begin
  if (not SameStr(iOrgId,EmptyStr)) and (iItems<>nil) then
  begin
    if iJustUse then
    begin
      Condition:=Format(string('%s=''%s'' and %s=1'),[TCmCustomerFileModel.cOrgIdKey,iOrgId,TCmCustomerFileModel.cCustomerStatusKey]);
    end else begin
      Condition:=Format(string('%s=''%s'''),[TCmCustomerFileModel.cOrgIdKey,iOrgId]);
    end;
    OrderBy:=TCmCustomerFileModel.cCustomerIdKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用TCmCustomerFileService.LoadAll方法失败！错误内容:传入的iOrgId或iItems为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmCustomerFileService.Use(const iItem:TCmCustomerFile; out oError:string):Boolean;
begin
  if (iItem<>nil) then
  begin
    if iItem.CustomerStatus=1 then
    begin
      iItem.CustomerStatus:=0;
      if not inherited Edit(iItem,oError) then
      begin
        oError:=Format(string('调用TCmCustomerFileService.Use方法失败！错误内容:%s'),[oError]);
        Result:=False;
        Exit;
      end;
    end else begin
      iItem.CustomerStatus:=1;
      if not inherited Edit(iItem,oError) then
      begin
        oError:=Format(string('调用TCmCustomerFileService.Use方法失败！错误内容:%s'),[oError]);
        Result:=False;
        Exit;
      end;
    end;
  end else begin
    oError:=Format(string('调用TCmCustomerFileService.Use方法失败！错误内容:传入的iItem为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmCustomerFileService.Add(const iItem:TCmCustomerFile; out oError:string; const dIsCheck:Boolean=True):Boolean;
var
  EnterpService:TCmEnterpriseFileService;
  EnterpItem:TCmEnterpriseFile;
  SQLs:TStrings;
begin
  if (iItem<>nil) then
  begin
  {$IFDEF SERVER}
    EnterpService:=TCmEnterpriseFileService.Create(fServConn,fDBConn,fDBID);
  {$ENDIF}
  {$IFDEF CLIENT}
    EnterpService:=TCmEnterpriseFileService.Create(fServConn);
  {$ENDIF}
    EnterpItem:=TCmEnterpriseFile.Create;
    SQLs:=TStringList.Create;
    try
      EnterpItem.EnterpriseId:=iItem.CustomerId;
      EnterpItem.EnterpriseFullName:=iItem.CustomerFullName;
      EnterpItem.EnterpriseSimpleName:=iItem.CustomerSimpleName;
      EnterpItem.EnterpriseStatus:=iItem.CustomerStatus;
      EnterpItem.EnterpriseType:=iItem.CustomerStatus;
      EnterpItem.EnterpriseSortNo:=iItem.CustomerSortNo;
      EnterpItem.EnterpriseTaxNo:=iItem.CustomerTaxNo;
      EnterpItem.RecordPsn:=iItem.RecordPsn;
      EnterpItem.RecordDate:=iItem.RecordDate;
      EnterpItem.TimeStamp:=iItem.TimeStamp;
      if inherited GetAddSQLs(iItem,SQLs,oError,dIsCheck) and
        EnterpService.GetAddSQLs(EnterpItem,SQLs,oError,dIsCheck) then
      begin
        if not TSK0001Comm.ExecuteSQLs(fDBConn,EmptyStr,SQLs,oError) then
        begin
          oError:=Format(string('调用TCmCustomerFileService.Add方法失败！错误内容:%s'),[oError]);
          Result:=False;
          Exit;
        end;
      end else begin
        oError:=Format(string('调用TCmCustomerFileService.Add方法失败！错误内容:%s'),[oError]);
        Result:=False;
        Exit;
      end;
    finally
      EnterpService.Free;
      EnterpItem.Free;
      SQLs.Free;
    end;
  end else begin
    oError:=Format(string('调用TCmCustomerFileService.Add方法失败！错误内容:传入的iItem为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmCustomerFileService.Delete(const iItem:TCmCustomerFile; out oError:string; const dIsCheck:Boolean):Boolean;
var
  SQLs:TStrings;
  SQL:string;
begin
  if (iItem<>nil) then
  begin
    SQLs:=TStringList.Create;
    try
      if inherited GetDeleteSQLs(iItem,SQLs,oError,dIsCheck) then
      begin
        SQL:=Format(string('DELETE FROM %s WHERE %s=''%s'' AND %s=''%s'' AND %s=%d;'),[TCmEnterpriseEmpModel.cTblName,
                                              TCmEnterpriseEmpModel.cEnterpriseIdKey,iItem.CustomerId,
                                              TCmEnterpriseEmpModel.cOrgIdKey,iItem.OrgId,
                                              TCmEnterpriseEmpModel.cEmpTypeKey,1]);
        SQLs.Add(SQL);
        SQL:=Format(string('DELETE FROM %s WHERE %s=''%s'' AND %s=''%s'' AND %s=%d;'),[TCmLinkmanModel.cTblName,
                                              TCmLinkmanModel.cEnterpriseIdKey,iItem.CustomerId,
                                              TCmLinkmanModel.cOrgIdKey,iItem.OrgId,
                                              TCmLinkmanModel.cLinkmanTypeKey,1]);
        SQLs.Add(SQL);
        SQL:=Format(string('DELETE FROM %s WHERE %s=''%s'';'),[TCmEnterpriseFileModel.cTblName,
                                              TCmEnterpriseFileModel.cEnterpriseIdKey,iItem.CustomerId]);
        SQLs.Add(SQL);
        if not TSK0001Comm.ExecuteSQLs(fDBConn,EmptyStr,SQLs,oError) then
        begin
          oError:=Format(string('调用TCmCustomerFileService.Delete方法失败！错误内容:%s'),[oError]);
          Result:=False;
          Exit;
        end;
      end else begin
        oError:=Format(string('调用TCmCustomerFileService.Delete方法失败！错误内容:%s'),[oError]);
        Result:=False;
        Exit;
      end;
    finally
      SQLs.Free;
    end;
  end else begin
    oError:=Format(string('调用TCmCustomerFileService.Delete方法失败！错误内容:传入的iItem为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;
  
end.
